package com.silence.gankio.mvvm.model

import com.silence.gankio.net.BaseBindingObserver
import com.silence.gankio.net.BaseModelObserver
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

open class BaseModel {

    private val mCompositeDisposable by lazy { CompositeDisposable() }

    fun applyDisposable(d: Disposable) {
        mCompositeDisposable.add(d)
    }

    fun clearDisposable() {
        mCompositeDisposable.clear()
    }

    protected fun <T> load(observable: Observable<T>, observer: BaseModelObserver<T>) {
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(observer)
    }

}